/*
 *@本JS功能：排课管理-手动排课
 *@本JS作者：杨锡君
 *@编写时间：2017年11月13日
 */
$(function(){
	var Alert1 = new Alert(); 
	Alert1.init();
	var confirm1 = new Confirm();
	var loading1 = new Loading();
	loading1.init();
	var $department = $(".department");
	var $profession = $(".profession");
	var $direct = $(".direct");
	var $teacher = $(".teacher");
	var $courseName = $(".courseName");
	var $coursePlace = $(".coursePlace");
	var $courseTable = $(".courseTable");
	var $nowWeek = $(".nowWeek");
	var $preWeek = $(".preWeek");
	var $nextWeek = $(".nextWeek");
	var $saveTable = $(".saveTable");
	var courseArr = [];
	function tableModel(opt){
		var mon = opt[0];
		var monday = mon.split(" ")[1];
		var mondate = mon.split(" ")[0];
		var tue = opt[1];
		var tuesday = tue.split(" ")[1];
		var tuedate = tue.split(" ")[0];
		var wed = opt[2];
		var wednesday = wed.split(" ")[1];
		var weddate = wed.split(" ")[0];
		var thu = opt[3];
		var thursday = thu.split(" ")[1];
		var thudate = thu.split(" ")[0];
		var fri = opt[4];
		var friday = fri.split(" ")[1];
		var fridate = fri.split(" ")[0];
		var sat = opt[5];
		var saturday = sat.split(" ")[1];
		var satdate = sat.split(" ")[0];
		var sun = opt[6];
		var sunday = sun.split(" ")[1];
		var sundate = sun.split(" ")[0];
		return "<tr>"
				+"<th></th>"	
				+"<th>"
				+"<div>"+ monday +"</div>"
				+"<div class=\"nowTime\">"+ mondate +"</div>"
				+"</th>"
				+"<th>"
				+"<div>"+ tuesday +"</div>"
				+"<div>"+ tuedate +"</div>"
				+"</th>"
				+"<th>"
				+"<div>"+ wednesday +"</div>"
				+"<div>"+ weddate +"</div>"
				+"</th>"
				+"<th>"
				+"<div>"+ thursday +"</div>"
				+"<div>"+ thudate +"</div>"
				+"</th>"
				+"<th>"
				+"<div>"+ friday +"</div>"
				+"<div>"+ fridate +"</div>"
				+"</th>"
				+"<th>"
				+"<div>"+ saturday +"</div>"
				+"<div>"+ satdate +"</div>"
				+"</th>"
				+"<th>"
				+"<div>"+ sunday +"</div>"
				+"<div>"+ sundate +"</div>"
				+"</th>"
				+"</tr>"
				+"<tr>"
				+"<td rowspan=\"2\">"
				+"<div class=\"fl\">上&emsp;午</div>"
				+"<div  class=\"fl\">"
				+"<div>第一节</div>"
				+"<div>第二节</div>"
				+"</div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"1\" data-date='"+ mondate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"1\" data-date='"+ tuedate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"1\" data-date='"+ weddate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"1\" data-date='"+ thudate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"1\" data-date='"+ fridate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"1\" data-date='"+ satdate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"1\" data-date='"+ sundate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"</tr>"
				+"<tr>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"2\" data-date='"+ mondate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"2\" data-date='"+ tuedate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"2\" data-date='"+ weddate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"2\" data-date='"+ thudate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"2\" data-date='"+ fridate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"2\" data-date='"+ satdate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"2\" data-date='"+ sundate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"</tr>"
				+"<tr>"
				+"<td rowspan=\"2\">"
				+"<div class=\"fl\">下&emsp;午</div>"
				+"<div  class=\"fl\">"
				+"<div>第三节</div>"
				+"<div>第四节</div>"
				+"</div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"3\" data-date='"+ mondate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"3\" data-date='"+ tuedate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"3\" data-date='"+ weddate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"3\" data-date='"+ thudate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"3\" data-date='"+ fridate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"3\" data-date='"+ satdate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"3\" data-date='"+ sundate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"</tr>"
				+"<tr>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"4\" data-date='"+ mondate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"4\" data-date='"+ tuedate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"4\" data-date='"+ weddate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"4\" data-date='"+ thudate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"4\" data-date='"+ fridate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"4\" data-date='"+ satdate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"4\" data-date='"+ sundate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"</tr>"
				+"<tr>"
				+"<td rowspan=\"2\">"
				+"<div class=\"fl\">晚&emsp;上</div>"
				+"<div  class=\"fl\">"
				+"<div>第五节</div>"
				+"</div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"5\" data-date='"+ mondate +"' class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"5\" data-date='"+ tuedate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"5\" data-date='"+ weddate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"5\" data-date='"+ thudate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"5\" data-date='"+ fridate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"5\" data-date='"+ satdate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"<td>"
				+"<div data-value=\"0\" data-num=\"5\" data-date='"+ sundate +"'  class=\"tableAddSection\"></div>"
				+"</td>"
				+"</tr>"; 
	}
	var courseArrangeMana = {
		init:function(){
			this.bindEvent();
		},
		bindEvent:function(){
			var self = this;
			self.departmentList();
			self.typeTap();
			self.tableEvent();
			self.weekTap();
			self.saveTableBtn();
		},
		saveTableBtn:function(){
//			保存手动排课
			$saveTable.off("click").click(function(){
				if(courseArr.length  == 0){
					Alert1.content = "未手动排课，请先手动排课后保存";
					Alert1.init();
					Alert1.show();
					return false;
				}else{
					var jsonStr = [];
					$.each(courseArr, function(key) {
						jsonStr.push({"classDate":this.date,"classNum":Number(this.num),"courseId":Number(this.courseid),"departmentId":Number(this.departmentid),"mainId":Number(this.mainid),"professionId":Number(this.professionid),"selectMark":Number($(".radioCheck").attr("data-value")),"siteId":Number(this.siteid),"stuorteaId":this.stuorteaid});
					});
					loading1.show();
					$.ajax({
						type:"post",
						url:"saveCourseTab",
						async:true,
						data:{
							"jsonStr":JSON.stringify({"tabList":jsonStr})
						},
						success:function(msg){
							loading1.hide();
							msg = JSON.parse(msg);
							if(msg.result == "success"){
								Alert1.content = "手动排课成功";
								Alert1.success = function(){
									var json = JSON.stringify({
										"pageName": "courseArrangeMana"
									});
									window.location.href = "skipPage?jsonStr=" + json;
								};
								Alert1.init();
								Alert1.show();
							}else{
								Alert1.content = msg.failMsg;
								Alert1.init();
								Alert1.show();
							}
						}
					});
				}
			});
		},
		coursePlaceChange:function(){
//			地点改变
			var self = this;
			$coursePlace.off("change").change(function(){
				if($(this).val() == 0){
					self.getTableData($(".nowTime").html(),0);
				}else{
					self.getTableData($(".nowTime").html(),$(this).val());
				}
			});
		},
		weekTap:function(){
			var self = this;
//			上下周本周切换
			$nowWeek.off("click").click(function(){
				if($coursePlace.val() == 0){
					self.getTableData(timeObject.changedate(),0);
				}else{
					self.getTableData(timeObject.changedate(),$coursePlace.val());
				}
			});
			$preWeek.off("click").click(function(){
				var date = new Date($(".nowTime").html()).getTime()-7*24*60*60*1000;
				var time = timeObject.changedateYear(date);
				if($coursePlace.val() == 0){
					self.getTableData(time,0);
				}else{
					self.getTableData(time,$coursePlace.val());
				}
			});
			$nextWeek.off("click").click(function(){
				var date = new Date($(".nowTime").html()).getTime()+7*24*60*60*1000;
				var time = timeObject.changedateYear(date);
				if($coursePlace.val() == 0){
					self.getTableData(time,0);
				}else{
					self.getTableData(time,$coursePlace.val());
				}
			});
		},
		tableClick:function(){
			// 课表点击
			var self = this;
			$(".tableAddSection").off("click").click(function(){
				var flag = self.verificationEvent();
				if(flag){
					if($(this).attr("data-value") == 0){
						$(this).attr({"data-value":"2","data-departmentid":$department.val(),"data-professionid":$profession.val(),"data-mainid":$direct.val(),"data-siteid":$coursePlace.val(),"data-stuorteaid":$teacher.val(),"data-courseid":$courseName.val()});
						$(this).css("background",$courseName.children("option:selected").attr("data-color")+" url(pc/images/ico_courseCheck.png) no-repeat right top").html($courseName.children("option:selected").html());
						courseArr.push({"color":$(this).attr("style"),"html":$(this).html(),"num":$(this).attr("data-num"),"date":$(this).attr("data-date"),"departmentid":$(this).attr("data-departmentid"),"professionid":$(this).attr("data-professionid"),"mainid":$(this).attr("data-mainid"),"siteid":$(this).attr("data-siteid"),"courseid":$(this).attr("data-courseid"),"stuorteaid":$(this).attr("data-stuorteaid")});
					}else if($(this).attr("data-value") == 2){
						for(var i = 0;i<courseArr.length;i++){
							if(courseArr[i].num == $(this).attr("data-num")&&courseArr[i].date == $(this).attr("data-date")){
								courseArr.splice(i,1);
							}
						}
						$(this).css("background","").html("").attr({"data-courseid":"","data-value":"0","data-departmentid":"","data-professionid":"","data-mainid":"","data-siteid":"","data-stuorteaid":"","data-courseid":""});
					}
					
				}
			});
		},
		verificationEvent:function(){
			if($department.val() == 0){
				Alert1.content = "请选择院系";
				Alert1.init();
				Alert1.show();
				return false;
			}
			if($profession.val() == 0){
				Alert1.content = "请选择专业";
				Alert1.init();
				Alert1.show();
				return false;
			}
			if($direct.val() == 0){
				Alert1.content = "请选择专业方向";
				Alert1.init();
				Alert1.show();
				return false;
			}
			if($teacher.val() == 0){
				Alert1.content = "请选择教师";
				Alert1.init();
				Alert1.show();
				return false;
			}
			if($courseName.val() == 0){
				Alert1.content = "请选择课程";
				Alert1.init();
				Alert1.show();
				return false;
			}
			if($coursePlace.val() == 0){
				Alert1.content = "请选择上课地点";
				Alert1.init();
				Alert1.show();
				return false;
			}
			return true;
		},
		getTableData:function(date,siteId){
//			获取已有课程
			var self = this;
			var jsonStr = {
					"nowDate":date
				};
			if(siteId != 0){
				jsonStr.siteId = Number(siteId);
			}	
			$.ajax({
				type:"post",
				url:"getLastAndAfterTab",
				async:true,
				data:{
					"jsonStr":JSON.stringify(jsonStr)
				},
				success:function(msg){
					msg = JSON.parse(msg);
					$courseTable.html(tableModel(msg.dateList));
					if(msg.classTab!=undefined){
                        if(msg.classTab.length != 0){
                            $.map(msg.classTab, function(opt) {
                                $.each($(".tableAddSection[data-date*='"+ opt.classDate.split(" ")[0] +"']"), function(key) {
                                    if($(this).attr("data-num") == opt.classNum){
                                        $(this).css("background",opt.courseColer).html(opt.courseName).attr({"data-courseid":opt.courseId,"data-value":"1"});
                                    }
                                });
                            });
                        }
					}

					if(courseArr.length != 0){
						
						$.each(courseArr, function(key) {
							var that = this;
							$.each($(".tableAddSection[data-date*='"+ this.date +"']"), function(key) {
								if($(this).attr("data-num") == that.num){
									$(this).html(that.html);
									$(this).attr({"data-courseid":that.courseid,"data-siteid":that.siteid,"data-mainid":that.mainid,"data-professionid":that.professionid,"data-departmentid":that.departmentid,"data-stuorteaid":that.stuorteaid,"data-value":"2","style":that.color});
								}
							});
						});
					}
					self.tableClick();
				}
			});
			
		},
		tableEvent:function(){
//			获取默认课表
			var self = this;
			$.ajax({
				type:"post",
				url:"skipScheduleMana",
				async:true,
				success:function(msg){
                    msg = JSON.parse(msg);
					$courseTable.html(tableModel(msg.dateList));
					if(msg.paramMap.selectCourseMark == 1){
							$(".female").show();
					}
                    if(msg.paramMap.selectCourseMark == 2){
                        $(".female").hide();
                    }
					self.tableClick();
				}
			});
		},
		typeTap:function(){
			//排课类型选择
			var self = this;
			$(".male").off("click").on("click", function() {
				$(this).removeClass("radio").addClass("radioCheck");
				$(".female").removeClass("radioCheck").addClass("radio");
				self.selectInit();
				self.tableEvent();
				courseArr = []
			});
			$(".female").off("click").on("click", function() {
				$(this).removeClass("radio").addClass("radioCheck");
				$(".male").removeClass("radioCheck").addClass("radio");
				self.selectInit();
				self.tableEvent();
				courseArr = []
			});
		},
		selectInit:function(){
			var self = this;
			$department.html("<option value=\"0\">请选择</option>");
			$profession.html("<option value=\"0\">请选择</option>");
			$direct.html("<option value=\"0\">请选择</option>");
			$teacher.html("<option value=\"0\">请选择</option>");
			$courseName.html("<option value=\"0\">请选择</option>");
			$coursePlace.html("<option value=\"0\">请选择</option>");
			self.departmentList();
		},
		departmentList:function(){
//			获取院系
			var self = this;
			$.ajax({
				type:"post",
				url:"getDepartmentListPost",
				async:true,
				success:function(msg){
                    msg = JSON.parse(msg);
					$department.empty();
					$department.append("<option value=\"0\">请选择</option>");
					if(msg.departmentList.length!=0){
						$.map(msg.departmentList, function(opt) {
							$department.append("<option value="+ opt.departmentId +">"+ opt.departmentName +"</option>");
						});
						self.departmentChange();
					}
				}
			});
		},
		departmentChange:function(){
//			院系专业联动
			var self = this;
			$department.off("change").change(function(){
				if($(this).val() == 0){
					$profession.html("<option value=\"0\">请选择</option>");
					$direct.html("<option value=\"0\">请选择</option>");
					$teacher.html("<option value=\"0\">请选择</option>");
					$courseName.html("<option value=\"0\">请选择</option>");
					$coursePlace.html("<option value=\"0\">请选择</option>");
					self.tableEvent();
				}else{
					var jsonStr = JSON.stringify({
						"departmentId":Number($(this).val())
					});
					$.ajax({
						type:"post",
						url:"getProfessionListByDepmentId",
						async:true,
						data:{
							"jsonStr":jsonStr
						},
						success:function(msg){
                            msg = JSON.parse(msg);
                            $profession.empty();
                            $profession.append("<option value=\"0\">请选择</option>");
							if(msg.professionList.length!=0){
								$.map(msg.professionList, function(opt) {
									$profession.append("<option value="+ opt.professionId +">"+ opt.professionName +"</option>");
								});
								self.professionChange();
							}
							
						}
					});
				}	
			});
		},
		professionChange:function(){
			var self = this;
//			专业联动专业方向与教师,课程及地点
			$profession.off("change").change(function(){
				if($(this).val() == 0){
					$direct.html("<option value=\"0\">请选择</option>");
					$teacher.html("<option value=\"0\">请选择</option>");
					$courseName.html("<option value=\"0\">请选择</option>");
					$coursePlace.html("<option value=\"0\">请选择</option>");
					self.tableEvent();
				}else{
					var jsonStr = JSON.stringify({
						"professionId":Number($(this).val())
					});
	//				专业方向
					$.ajax({
						type:"post",
						url:"getMyProCourseList",
						async:true,
						data:{
							"jsonStr":jsonStr
						},
						success:function(msg){
                            msg = JSON.parse(msg);
	//						专业方向
                            $direct.empty();
                            $direct.append("<option value=\"0\">请选择</option>");
							if(msg.directList.length!=0){

								$.map(msg.directList, function(opt) {
									$direct.append("<option value="+ opt.mainId +">"+ opt.directName +"</option>");
								});
							}
							//				教师
                            $teacher.empty();
                            $teacher.append("<option value=\"0\">请选择</option>");
							if(msg.userList.length!=0){

								$.map(msg.userList, function(opt) {
									$teacher.append("<option value="+ opt.stuorteaId +">"+ opt.userName +"</option>");
								});
								//判断排课类型 得出课程列表
								if($(".radioCheck").attr("data-sex") == "m"){
									self.teacherChange();
								}else{
                                    $courseName.empty();
                                    $courseName.append("<option value=\"0\">请选择</option>");
									if(msg.courseListAll.length!=0){

										$.map(msg.courseListAll, function(opt) {
											$courseName.append("<option data-courseTitle="+ opt.courseTitle +"	data-color="+ opt.courseColer +" value="+ opt.courseId +">"+ opt.courseName +"</option>");
										});
									}
								}
								
							}
                            $coursePlace.empty();
                            $coursePlace.append("<option value=\"0\">请选择</option>");
							if(msg.siteList.length!=0){
								//						地点
								$.map(msg.siteList, function(opt) {
									$coursePlace.append("<option value="+ opt.siteId +">"+ opt.siteName +"</option>");
								});
								self.coursePlaceChange();
							}
						}
					});
				}

			});
		},
		teacherChange:function(){
			//教师联动课程
			var self = this;
			$teacher.off("change").change(function(){
                if($(".radioCheck").attr("data-sex") == "m"){
                    if($(this).val() == 0){
                        $courseName.html("<option value=\"0\">请选择</option>");
                    }else{
                        var jsonStr = JSON.stringify({
                            "stuorteaId":$(this).val(),
                            "couSelectMark":1
                        });
                        $.ajax({
                            type:"post",
                            url:"getMyClasses",
                            async:true,
                            data:{
                                "jsonStr":jsonStr
                            },
                            success:function(msg){
                                msg = JSON.parse(msg);
                                $courseName.empty();
                                $courseName.append("<option value=\"0\">请选择</option>");
                                if(msg.courseList.length!=0){

                                    $.map(msg.courseList, function(opt) {
                                        $courseName.append("<option data-courseTitle="+ opt.courseTitle +"	data-color="+ opt.courseColer +" value="+ opt.courseId +">"+ opt.courseName +"</option>");
                                    });
                                }
                            }
                        });
                    }
				}

				
			});
		}
	};
	courseArrangeMana.init();
});
